/*
** client.c for  in /home/baraze_l/tek2/project/system_unix/my_ftp/TP
**
** Made by leo barazer
** Login   <baraze_l@epitech.net>
**
** Started on  Mon Apr  08 17:51:32 2013 leo barazer
** Last update Wed Apr 10 16:02:03 2013 mickael gomes
*/

#include 	<sys/types.h>
#include 	<sys/socket.h>
#include 	<netdb.h>
#include 	<stdlib.h>
#include 	<netinet/in.h>
#include 	<arpa/inet.h>
#include 	<string.h>
#include 	<stdio.h>
#include 	<unistd.h>

void 		clear_buffer(char *buffer)
{
	int 	i;

	for (i = 0; i < 2048; ++i)
	{
		buffer[i] = 0;
	}
}

int 		main(int ac, char **av)
{
  int 				fd;
  struct protoent 	*pe;
  struct sockaddr_in 	s_addr;
  int 				port;
  char 				*ip;
  char 				buffer[2048];
  
  if (ac != 3)
    return (1);
  ip = av[2];
  port = atoi(av[1]);
  pe = getprotobyname("TCP");
  if (!pe)
    return (1);
  s_addr.sin_family = AF_INET;
  s_addr.sin_port = htons(port);
  s_addr.sin_addr.s_addr = inet_addr(ip);
  fd = socket(AF_INET, SOCK_STREAM, pe->p_proto);
  if (!fd)
    return (1);
  connect(fd, (struct sockaddr *)&s_addr, sizeof(s_addr));
  int			len = 0;
  while(42)
    {
      len = read(0, buffer, 2048);
      buffer[len] = '\0';
      write(fd, buffer, len);
      clear_buffer(buffer);
    }
  close(fd);
  return (0);
}
